﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class station : FormBase1
    {
        private SVC.client.Station selected = null;
        private BindingList<SVC.client.Power> powers;
        public station()
        {
            InitializeComponent();
            Init();
            gridControl1.DataSource = my.bw.Station_Get("",my.ACID);
            gridView1.FocusedRowHandle = -1;
        }

        private void btn_new_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            station_N frm = new station_N();
            frm.FatherRefresh = bind;
            frm.ShowDialog();
        }

        private void station_Load(object sender, EventArgs e)
        {
            bind();

        }

        private void Init()
        {

            flowLayoutPanel1.Controls.Clear();

            UserControls.ModelPower upmp = null;
            foreach (SVC.client.model model in my.bw.model_Get("",my.ACID))
            {
                UserControls.ModelPower mp = new UserControls.ModelPower();
                mp.Powers = my.bw.Getpower_Bymodelid(model.modelid,my.ACID);
                mp.Name = model.ModelName;
                mp.modelid = model.modelid;
                mp.模块名称 = model.ModelName;
                mp.Init();

                mp.Left = 10;
                if (upmp == null)
                {
                    mp.Top = 30;

                }
                else
                {
                    mp.Top = upmp.Location.Y + mp.Height + 5;
                }
                upmp = mp;
                //flowLayoutPanel2.Controls.Find(model.ModelName, false);
                flowLayoutPanel1.Controls.Add(mp);
                flowLayoutPanel1.Show();
            }
        }

        private void bind()
        {
            gridControl1.DataSource = my.bw.Station_Get("", my.ACID);
            powers = my.bw.Power_Get("", my.ACID);
        }

        void BindPower(SVC.client.Station stt)
        {
            foreach (UserControls.ModelPower mp in flowLayoutPanel1.Controls)
            {
                mp.HasPowers = new List<int>();
                string pw = my.bw.GetPower_ByStation(stt.StationCode, my.ACID);
                string[] ps = pw.Split('|');
                foreach(string pwid in ps)
                {
                    if (!string.IsNullOrEmpty(pwid))
                    {
                        if (powers.Single(p => p.powerid == int.Parse(pwid)).modelid == mp.modelid)
                        {
                            mp.HasPowers.Add(int.Parse(pwid));
                        }
                    }
                    
                }
                //if (focusgp.GroupName == "系统管理组")
                //{
                //    mp.ReadOnly = true;
                //}
                //else
                //{
                //    mp.ReadOnly = false;
                //}
                mp.岗位编码 = stt.StationCode;
                mp.Bind();
            }
        }

        private void btn_sx_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bind();
        }

        private void btn_delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle>=0)
            {
                SVC.client.Station st = gridView1.GetRow(gridView1.FocusedRowHandle) as SVC.client.Station;
                if (st!=null)
                {
                    if (DialogResult.OK != XtraMessageBox.Show("删除后不可恢复，确定要删除？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button2)) return;
                    string r = my.bw.StationByStationCode_Delete(st.StationCode, my.ACID);
                    if (!string.IsNullOrEmpty(r))
                    {
                        Comm.ShowErorr(r);
                    }
                    else
                    {
                        bind();
                    }
                }
            }
        }

        private void btn_update_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                SVC.client.Station st = gridView1.GetRow(gridView1.FocusedRowHandle) as SVC.client.Station;
                if (st != null)
                {
                    station_U frm = new station_U(st);
                    frm.FatherRefresh = bind;
                    frm.ShowDialog();
                }
            }
        }

        private void btn_mm_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                SVC.client.Employee emp = gridView1.GetRow(gridView1.FocusedRowHandle) as SVC.client.Employee;
                if (emp != null)
                {
                    Emp_U frm = new Emp_U(emp);
                    frm.FatherRefresh = bind;
                    frm.ShowDialog();
                }
            }
        }

        private void btn_stationpower_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selected==null) 
            {
                Comm.ShowAlert("请先选择一个岗位");
                return;
            }
            string str="";
            foreach (UserControls.ModelPower mp in flowLayoutPanel1.Controls)
            {
                if (!string.IsNullOrEmpty(str) && mp.HasPowers.Count>0)
                {
                    str += "|";
                }
                for (int i = 0; i < mp.HasPowers.Count; i++)
                {

                    str += mp.HasPowers[i];
                    if (i!=(mp.HasPowers.Count-1))
                    {
                        str += "|";
                    }
                }
            }
            SVC.client.Station st = gridView1.GetRow(gridView1.FocusedRowHandle) as SVC.client.Station;
            st.powers = str;
            Comm.ShowAlert(str);
            string r = my.bw.Station_Update(st, my.ACID);
            if (!string.IsNullOrEmpty(r))
            {
                Comm.ShowErorr(r);
            }
            else
            {
                Comm.ShowInfo("修改岗位"+st.StationName+"的权限成功！");
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle>=0)
            {
                SVC.client.Station st = gridView1.GetRow(gridView1.FocusedRowHandle) as SVC.client.Station;
                groupControl1.Text = string.Format("岗位【{0}】权限列表",st.StationName);
                BindPower(st);
                selected = st;
            }
        }

        private void btn_stationEMP_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
    }
}